package Graph;


import java.util.Vector;

public class DenseGraph implements Graph{

    private int n;
    private int m;
    private boolean directed;
    private boolean[][] g;

    public DenseGraph(int n, boolean directed) {
        this.n = n;
        this.m = 0;
        this.directed = directed;

        g = new boolean[n][n];

    }

    public int V() {
        return n;
    } // 返回节点个数

    public int E() {
        return m;
    } // 返回边的个数

    public void addEdge(int v, int w) {
        assert v > 0 && v < n;
        assert w > 0 && w < n;

        if (hasEdge(v, w)) {
            return;
        }

        g[v][w] = true;
        if (!directed) {
            g[w][v] = true;
        }

        m++;
    }

    public boolean hasEdge(int v, int w) {
        return g[v][w];
    }

    // 返回图中一个顶点的所有邻边
    public Iterable<Integer> adj(int v){
        assert v > 0 && v < n;
        Vector<Integer> vector = new Vector<>();
        for(int i=0;i<n;i++){
            if(g[v][i] == true){
                vector.add(i);
            }
        }

        return vector;
    }

    // 显示图的信息
    public void show(){

        for( int i = 0 ; i < n ; i ++ ){
            for( int j = 0 ; j < n ; j ++ )
                System.out.print(g[i][j]+"\t");
            System.out.println();
        }
    }
}
